#include "widget_avatar_view.h"
#include "../AvatarLibrary.h"
#include "../Program.h"
#include <QResizeEvent>
#include <QDebug>

WidgetAvatarView::WidgetAvatarView(QWidget *parent)
    : QWidget(parent, Qt::MSWindowsOwnDC)
{
    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_NoSystemBackground);
    setAttribute(Qt::WA_InputMethodEnabled);
    setFocusPolicy(Qt::StrongFocus);
    setMouseTracking(true);
    _keyPressedU = false;
    _keyPressedD = false;
    _keyPressedL = false;
    _keyPressedR = false;
}

WidgetAvatarView::~WidgetAvatarView()
{
    _timer.stop();
    AvatarLibrary::avatar_exit();
}

/**
 * @brief 启动引擎
 * @param directory
 */
void WidgetAvatarView::Start(const QString& directory)
{
    const QString script = R"(
        setCameraTarget = function(name)
            local minx, miny, minz, maxx, maxy, maxz = scene.boundingBox(name)
            if minx < maxx then
                local cx = (minx + maxx) / 2
                local cy = (miny + maxy) / 2
                local cz = (minz + maxz) / 2
                camera.target(cx, cy, cz)
                camera.position(cx, 2 * miny - maxy, 2 * maxz - minz)
                camera.target(cx, cy, cz)
            end
        end)";
    AvatarLibrary::avatar_init(directory, (void*)this->winId());
    AvatarLibrary::avatar_command(QString("graphics.background(%1,1.0)").arg(Program::Config.Background.ToString()));
    AvatarLibrary::avatar_command(script);
    resizeEvent(nullptr);
    _timer.start(12, this);
}

/**
 * @brief 获取屏幕大小
 * @return
 */
QSize WidgetAvatarView::ScreenSize()
{
    return QSize(width() * devicePixelRatio(), height() * devicePixelRatio());
}

void WidgetAvatarView::timerEvent(QTimerEvent *event)
{
    update();
}

void WidgetAvatarView::paintEvent(QPaintEvent *event)
{
    const float moveSpeed = 5.0f;
    float mx = 0.0f;
    float my = 0.0f;
    float mz = 0.0f;
    if (_keyPressedU) my = moveSpeed;
    else if (_keyPressedD) my = -moveSpeed;
    if (_keyPressedL) mx = -moveSpeed;
    else if (_keyPressedR) mx = moveSpeed;
    AvatarLibrary::avatar_move(mx, my, mz);
    AvatarLibrary::avatar_update();
}

void WidgetAvatarView::resizeEvent(QResizeEvent *event)
{
    AvatarLibrary::avatar_resize(width() * devicePixelRatio(), height() * devicePixelRatio());
}

void WidgetAvatarView::mousePressEvent(QMouseEvent *event)
{
    int button;
    if (event->button() == Qt::LeftButton) button = 1;
    else if (event->button() == Qt::RightButton) button = 2;
    else if (event->button()== Qt::MiddleButton) button = 3;
    else button = 0;
    QPoint pos = event->position().toPoint() * devicePixelRatio();
    AvatarLibrary::avatar_mouse(pos.x(), pos.y(), button, 0);
}

void WidgetAvatarView::mouseReleaseEvent(QMouseEvent *event)
{
    QPoint pos = event->position().toPoint() * devicePixelRatio();
    AvatarLibrary::avatar_mouse(pos.x(), pos.y(), 0, 0);
}

void WidgetAvatarView::mouseMoveEvent(QMouseEvent *event)
{
    int button;
    if (event->buttons() & Qt::LeftButton) button = 1;
    else if (event->buttons() & Qt::RightButton) button = 2;
    else if (event->buttons() & Qt::MiddleButton) button = 3;
    else button = 0;
    QPoint pos = event->position().toPoint() * devicePixelRatio();
    AvatarLibrary::avatar_mouse(pos.x(), pos.y(), button, 0);
}

void WidgetAvatarView::wheelEvent(QWheelEvent *event)
{
    QPoint pos = event->position().toPoint() * devicePixelRatio();
    AvatarLibrary::avatar_mouse(pos.x(), pos.y(), 0, event->angleDelta().y() / 120);
}

void WidgetAvatarView::keyPressEvent(QKeyEvent *event)
{
    QString inputText = event->text();
    for (qsizetype i = 0; i < inputText.length(); i++)
    {
        AvatarLibrary::avatar_keyboard((int)inputText.at(i).unicode());
    }
    switch (event->key())
    {
    case Qt::Key_Up: _keyPressedU = true; break;
    case Qt::Key_Down: _keyPressedD = true; break;
    case Qt::Key_Left: _keyPressedL = true; break;
    case Qt::Key_Right: _keyPressedR = true; break;
    default: break;
    }
}

void WidgetAvatarView::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Up: _keyPressedU = false; break;
    case Qt::Key_Down: _keyPressedD = false; break;
    case Qt::Key_Left: _keyPressedL = false; break;
    case Qt::Key_Right: _keyPressedR = false; break;
    default: break;
    }
}

void WidgetAvatarView::inputMethodEvent(QInputMethodEvent* event)
{
    QString inputText = event->commitString();
    for (qsizetype i = 0; i < inputText.length(); i++)
    {
        AvatarLibrary::avatar_keyboard((int)inputText.at(i).unicode());
    }
}

void WidgetAvatarView::focusOutEvent(QFocusEvent *event)
{
    _keyPressedU = false;
    _keyPressedD = false;
    _keyPressedL = false;
    _keyPressedR = false;
}
